/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.solarchallenge.client.gwtui.applicants.register.agreement;

import static com.google.solarchallenge.client.rpc.ServiceProvider.getServiceProvider;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.solarchallenge.client.event.EventCategory;
import com.google.solarchallenge.client.event.HistoryHandler;
import com.google.solarchallenge.client.gwtui.mvpinterfaces.Presenter;
import com.google.solarchallenge.shared.dtos.UserAccountDto;

/**
 * Presenter for ApplicantRegisterAgreement UI.
 *
 * @author Arjun Satyapal
 */
public class ApplicantRegisterAgreementPresenter extends Presenter {
  private ApplicantRegisterAgreementDisplay display;

  public ApplicantRegisterAgreementPresenter(
      ApplicantRegisterAgreementDisplay display, String historyToken) {
    super(display.getSolarChallengeHeaderPanel(), historyToken);
    this.display = display;
  }

  @Override
  public void go(HasWidgets container) {
    container.clear();
    container.add(display.asWidget());
    bind();
  }

  @Override
  public void bind() {
    display.getButtonAgree().addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        getUserAccountAndAcceptAgreement(true /* isAccepted */);
      }

    });

    display.getButtonDoNotAgree().addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        getUserAccountAndAcceptAgreement(false /* isAccepted */);
      }
    });
  }

  private void getUserAccountAndAcceptAgreement(final boolean isAccepted) {
    display.setEnabled(false);
    AsyncCallback<UserAccountDto> callback
        = new AsyncCallback<UserAccountDto>() {
      @Override
      public void onSuccess(UserAccountDto result) {
        updateApplicantAgreementStatus(result.getKey(), isAccepted);
      }

      @Override
      public void onFailure(Throwable caught) {
        display.setEnabled(true);
        Window
            .alert("Failed to get User Session status due to reason[" + caught
                .getLocalizedMessage() + "].");
      }
    };
    getServiceProvider().getLoginService().getLoggedInUserDTO(callback);
  }

  private void updateApplicantAgreementStatus(String userAccountKey,
      boolean isAccepted) {
    AsyncCallback<Void> callback = new AsyncCallback<Void>() {
      @Override
      public void onSuccess(Void result) {
        HistoryHandler.handleNewToken(EventCategory.HOME.getToken());
      }

      @Override
      public void onFailure(Throwable caught) {
        Window
            .alert("Failed to update AgreementStatus on Server due to reason["
                + caught.getLocalizedMessage() + "].");
      }
    };

    getServiceProvider().getApplicantService().updateApplicantAgreementStatus(
        userAccountKey, isAccepted, callback);
  }
}
